package cn.ffcs.uec.outStaff.component;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Window;

import cn.ffcs.uec.outStaff.component.bean.UecOutStaffInfoListboxCompBean;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfoVo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * 组织树下外包员工管理
 * 
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年8月2日
 */
@Controller
@Scope("prototype")
public class UecOutStaffInfoListboxComp extends Div implements IdSpace {

	private static final long serialVersionUID = 7126642255547123017L;

	/**
	 * bean.
	 */
	private UecOutStaffInfoListboxCompBean bean = new UecOutStaffInfoListboxCompBean();

	/**
	 * zul.
	 */
	private final String zul = "/pages_nmg/uec_outStaff/comp/uec_outstaff_info_listbox.zul";

	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");

	/**
	 * UecStaffInfo
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 外包人员虚拟对象
	 */
	private UecOutStaffInfoVo uecOutStaffInfoVo;

	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 
	 */
	private boolean assesmentFlag = false;
	
	/**
	 * 
	 */
	private boolean assesmentCleanFlag = true;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOutStaffInfoListboxComp() {
		Executions.createComponents(this.zul, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');
		// 转发
		this.addForward("onOrgTreeOutStaffQuery", this, "onOrgTreeOutStaffQueryResponse");
		this.addForward("onAssesmentFlag", this, "onAssesmentFlagResponse");
	}

	/**
	 * 初始化
	 * 
	 * @throws ParseException
	 */
	public void onCreate() throws ParseException {
		// 防止直接查询报错
		uecOutStaffInfoVo = new UecOutStaffInfoVo();
		
		onQueryOutStaff();
	}

	/**
	 * 接收抛出的查询事件
	 * 
	 * @param event
	 */
	public void onOrgTreeOutStaffQueryResponse(final ForwardEvent event) {
		uecOutStaffInfoVo = (UecOutStaffInfoVo) event.getOrigin().getData();
		if (null != uecOutStaffInfoVo) {
			this.bean.getName().setValue(null);
			this.bean.getCode().setValue(null);
			this.bean.getIdNumber().setValue(null);
			uecOutStaffInfoVo.setStaffName(null);
			uecOutStaffInfoVo.setStaffCode(null);
			uecOutStaffInfoVo.setIdNum(null);
			// 执行查询
			onQueryOutStaff();
		}
	}
	
	public void onAssesmentFlagResponse(final ForwardEvent event) {
		assesmentFlag = (Boolean) event.getOrigin().getData();
	}

	/**
	 * onDoubleClick=onUecOutStaffInfoSelectRequest 双击选择员工.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onUecOutStaffInfoSelectRequest() throws Exception {
		if (this.bean.getUecOutStaffInfoListbox().getSelectedCount() > 0) {
			uecOutStaffInfoVo = (UecOutStaffInfoVo) bean.getUecOutStaffInfoListbox().getSelectedItem().getValue();
			if (uecOutStaffInfoVo != null) {
				/**
				 * 将获取到的虚拟对象转化为UecOutStaffInfo
				 */
				uecOutStaffInfo = UecOutStaffInfo.newInstance();
				uecOutStaffInfo.setOutStaffId(uecOutStaffInfoVo.getOutStaffId());
				uecOutStaffInfo.setPsnCode(uecOutStaffInfoVo.getStaffCode());
				uecOutStaffInfo = uecOutStaffManager.findUecOutStaffInfo(uecOutStaffInfo);
				if (null != uecOutStaffInfo) {
					if(!assesmentFlag) {
						onUecOutStaffInfoView();
					}else {
					    //将事件抛给父类--uecOutStaffAssesmentMainComposer
						Events.postEvent("onAssesmentUecStaffInfoQuery", this, uecOutStaffInfo);
					}
				}
			}
		}
	}

	/**
	 * 每页显示
	 * @throws Exception
	 */
	public void onPageSizeSelect() throws Exception {
		if (this.bean.getPageListbox() != null && this.bean.getPageListbox().getSelectedItem().getValue() != null) {
			this.bean.getUecOutStaffInfoListPaging().setActivePage(0);
			this.bean.getUecOutStaffInfoListPaging()
					.setPageSize(Integer.parseInt(this.bean.getPageListbox().getSelectedItem().getValue().toString()));
		}
		this.onQueryUecOutStaffInfo();
	}

	/**
	 * 重置按钮
	 */
	public void onResetOutStaff() {
		clearVariable();
		this.bean.getTotalSize().setValue(null);
		this.bean.getName().setValue(null);
		this.bean.getCode().setValue(null);
		this.bean.getIdNumber().setValue(null);
		ListboxUtils.clearListbox(this.bean.getUecOutStaffInfoListbox());
		this.bean.getUecOutStaffInfoListPaging().setTotalSize(1);
		// 防止重置后直接查询报错
		uecOutStaffInfoVo = new UecOutStaffInfoVo();
		Events.postEvent("onCleanAssesmentUecStaffInfoQuery", this, assesmentCleanFlag);
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecOutStaffInfoVo = null;
		uecOutStaffInfo = null;
	}

	/**
	 * 查询按钮
	 */
	public void onQueryOutStaff() {
		if (!StrUtil.isEmpty(this.bean.getCode().getValue())) {
			uecOutStaffInfoVo.setStaffCode(this.bean.getCode().getValue());
		}
		if (!StrUtil.isEmpty(this.bean.getName().getValue())) {
			uecOutStaffInfoVo.setStaffName(this.bean.getName().getValue());
		}
		if (!StrUtil.isEmpty(this.bean.getIdNumber().getValue())) {
			uecOutStaffInfoVo.setIdNum(this.bean.getIdNumber().getValue());
		}
		onQueryUecOutStaffInfo();
	}

	/**
	 * 查询员工信息
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onQueryUecOutStaffInfo() {
		if (this.uecOutStaffInfoVo != null) {
			int activePage = bean.getUecOutStaffInfoListPaging().getActivePage();
			int pageSize = bean.getUecOutStaffInfoListPaging().getPageSize();
			PageInfo pageInfo = uecOutStaffManager.queryPageInfoByUecOutStaffInfoVo(uecOutStaffInfoVo, activePage + 1, pageSize);
			ListModel list = null;
			//总条数
			bean.getTotalSize().setValue("0");
			if(pageInfo !=null) {
				list=	new BindingListModelList(pageInfo.getDataList(), true);
				//总条数
				bean.getTotalSize().setValue(String.valueOf(pageInfo.getTotalCount()));
				this.bean.getUecOutStaffInfoListPaging().setTotalSize(pageInfo.getTotalCount());
			}
			this.bean.getUecOutStaffInfoListbox().setModel(list);
			
		}
	}

	/**
	 * 查看员工信息
	 * 
	 * @throws Exception
	 */
	public void onUecOutStaffInfoView() throws Exception {
		opType = "view";
		this.openUecOutStaffInfoViewWin();
	}

	/**
	 * 打开员工信息查看窗口.
	 * 
	 * @param opType
	 *            操作类型
	 * @throws Exception
	 *             异常
	 */
	private void openUecOutStaffInfoViewWin() throws Exception {
		Map<String, Object> arg = new HashMap<String, Object>();
		arg.put("opType", opType);
		arg.put("uecOutStaffInfo", uecOutStaffInfo);
		Window win = (Window) Executions.createComponents("/pages_nmg/uec_outStaff/uec_outStaff_main.zul", this, arg);
		win.doModal();
	}
}
